﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LCA.Community.DAL.Model;
using Microsoft.Practices.Unity;
using LCA.Community.DAL.Interfaces;

namespace LCA.Community.Web.Models
{
    public class PostViewModel
    {
        private List<PostVote> _votes;
        private List<CommentViewModel> _comment;

        public PostViewModel(Post post)
        {
            Post = post;
        }

        public bool HasVoted(string userID)
        {
            return PostVotesRepository.UserHasVotedPost(userID, Post.RowKey);
        }

        public static List<PostViewModel> ConvertListPost(List<Post> posts)
        {
            var viewModels = new List<PostViewModel>();
            posts.ForEach(p => viewModels.Add(MvcApplication.Container.Resolve<PostViewModel>(new ParameterOverrides { { "post", p } })));

            return viewModels;
        }

        public Post Post { get; set; }
        public List<PostVote> Votes 
        {
            get
            {
                if (_votes == null)
                    _votes = VotesRepository.GetPostVotesByPost(Post.RowKey);

                return _votes;
            }
        }
        public List<CommentViewModel> Comments
        {
            get
            {
                if (_comment == null)
                {
                    _comment = CommentViewModel.ConvertListComments(CommentsRepository.GetCommentsByPost(Post.RowKey));
                }

                return _comment;
            }
        }

        [Dependency]
        public IPostVotesRepository VotesRepository { get; set; }
        [Dependency]
        public ICommentsRepository CommentsRepository { get; set; }
        [Dependency]
        public IPostVotesRepository PostVotesRepository { get; set; }
    }
}